package com.phospherus.flow.controller

import com.phospherus.common.controller.BaseController
import com.phospherus.common.controller.handler.PageData
import com.phospherus.common.controller.handler.ResultHandler
import com.phospherus.common.service.FormInstExportService
import com.phospherus.flow.entity.FormInst
import com.phospherus.flow.entity.FormItemPermission
import com.phospherus.flow.service.FormInstService
import com.phospherus.flow.service.FormQueryService
import org.apache.commons.io.FileUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import java.io.File
import java.io.IOException
import java.util.*
import javax.servlet.http.HttpServletRequest

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/12/21
 */
@Controller
@RequestMapping("/flow/formInst")
class FormInstController : BaseController<FormInst, FormInstService>() {
    @Value("\${upload.baseDirectory}")
    lateinit var baseDirectory: String

    @Autowired
    lateinit var formInstExportService: FormInstExportService

    @Autowired
    lateinit var formQuerService: FormQueryService
    @RequestMapping("/temporary")
    @ResponseBody
    fun temporary(@RequestBody map: MutableMap<String, Any>, request: HttpServletRequest): ResultHandler<Any> {
        map["USER"] = request.session.getAttribute("USER")
        if (!service.temporary(map)) {
            return ResultHandler(ResultHandler.SystemMessage.FAIL)
        }
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS)
    }

    @RequestMapping("/getFormInstData")
    @ResponseBody
    fun query(formInstId: Long, formServiceName: String, isHive: String?): ResultHandler<MutableMap<String, Any?>?> {
        var resultMap = service.getFormInstData(formInstId, formServiceName,isHive)
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, resultMap)
    }

    @RequestMapping("/exportExcel")
    @ResponseBody
    fun exportExcel(formInstId: Long, formServiceName: String, isHive: String?, moduleName: String, fileName: String): ResultHandler<Any> {
        var dataMap = service.getFormInstData(formInstId, formServiceName,isHive)
        val filePath = "excel/" + UUID.randomUUID().toString() + ".xlsx";

        formInstExportService.export(dataMap, moduleName, File(baseDirectory, filePath))

        var resultMap = HashMap<String, Any>()
        resultMap.put("filePath", filePath)
        resultMap.put("fileName", fileName)
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, resultMap)
    }
    @RequestMapping("/downloadExcel")
    fun downloadExcel(filePath: String, fileName: String): ResponseEntity<*> {
        val httpHeaders = HttpHeaders()
        httpHeaders.setContentDispositionFormData("attachment", String(fileName.toByteArray(charset("UTF-8")), charset("ISO8859-1")))
        httpHeaders.contentType = MediaType.APPLICATION_OCTET_STREAM
        val bytes = FileUtils.readFileToByteArray(File(baseDirectory, filePath))
        return ResponseEntity(bytes, httpHeaders, HttpStatus.OK)
    }

    @RequestMapping("/getPermission")
    @ResponseBody
    fun getPermissionByNodeId(flowId: Long,currNodeId: Long?): ResultHandler<FormItemPermission> {
        var resultMap = service.getPermission(flowId,currNodeId)
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, resultMap)
    }
    @RequestMapping("/send")
    @ResponseBody
    fun send(@RequestBody map: MutableMap<String, Any>, request: HttpServletRequest): ResultHandler<Any> {
        map["USER"] = request.session.getAttribute("USER")
        if (!service.send(map)) {
            return ResultHandler(ResultHandler.SystemMessage.FAIL)
        }
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS)
    }

    @RequestMapping("/todoList")
    @ResponseBody
    fun todoList(@RequestParam map: MutableMap<String, Any>, request: HttpServletRequest): ResultHandler<Any> {
        map["USER"] = request.session.getAttribute("USER")
        return ResultHandler(ResultHandler.SystemMessage.SUCCESS, PageData(formQuerService.getToDoLit(map)))
    }
}